﻿using System.Web.Mvc;
using NHibernate;
using SOLInfo.GEN.Domain.Entities;
using SOLInfo.GEN.Repository;
using SOLInfo.GEN.Repository.Config;

namespace SOLInfo.GEN.WebMVC.Controllers
{
    public class NoticiaController : Controller
    {
		ISession session;
        IRepository<Noticia> repo;

		public NoticiaController()
		{
            session = AutoConfiguracao.session.OpenSession();
            repo = new NoticiaRepository(session);
		}

        //
        // GET: /Noticia/

        public ActionResult Index()
        {
			var colecao = repo.ListarTodos();
            return View(colecao);
        }

        //
        // GET: /Noticia/Details/5

        public ActionResult Details(int Codigo)
        {
			var entidade = GetById(Codigo);
            return View(entidade);
        }

        //
        // GET: /Noticia/Create

        public ActionResult Create()
        {
            return View();
        } 

        //
        // POST: /Noticia/Create

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Create(FormCollection collection)
        {
            try
            {
                Gravar(0, collection);
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /Noticia/Edit/5
 
        public ActionResult Edit(int Codigo)
        {
			var entidade = GetById(Codigo);
            return View(entidade);
        }

        //
        // POST: /Noticia/Edit/5

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Edit(int Codigo, FormCollection collection)
        {
            try
            {
                Gravar(Codigo, collection); 
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

		public ActionResult Remove(int Codigo)
        {
            return RedirectToAction("Index");
        }

		private Noticia GetById(int Codigo)
		{
			Noticia entidade = new Noticia();
			return entidade;
		}

		private void Gravar(int id, FormCollection collection)
        {
			/* Criar aqui a inst�ncia */
            if(id>0)
			{
				/* Definir aqui o valor do c�digo */
			}

        }
    }
}
